function moveResTable(el,resId){
	$(el).parents("#BlankDialog").dialog("close");
	var elObj = $(".Table_Pos [posresid="+resId+"]");
	var newEl = $(elObj).clone();
	var elHeight = $(elObj).height();
	var newElHeight = $(elObj).parents(".Table_Pos").height();
	
	document.moveTable = {};
	document.moveTable.el = $(elObj);
	document.moveTable.newEl = newEl;
	document.moveTable.elOffset = $(elObj).offset();
	document.moveTable.initHeight = elHeight;
	
	$('body').append(newEl);
	newEl.removeAttr("onclick");
	newEl.css("border","1px solid #6b88c5");
	newEl.attr("dragable","true");
	newEl.css("-webkit-filter", "brightness(1.1)");
	newEl.css("font-size", "12px");
	newEl.css("position", "absolute");
	newEl.css("height",elHeight);
	newEl.css("max-height" , newElHeight);
	newEl.css("width" , $(elObj).width());
	newEl.css("top", $(elObj).offset().top);
	newEl.css("left", $(elObj).offset().left);
	newEl.css("cursor","-webkit-grab");
	newEl.css("cursor","-moz-grab");
	
	$(elObj).css("display","none");
	$(elObj).after("<div name='tmpBlock' style='height:"+elHeight+"px;background:#F6F6F6'></div>");
	newEl.draggable({
		drag: function(event,ui) {
			var midX = $(event.target).width()/2 + ui.offset.left;
			var midY = $(event.target).height()/2 + ui.offset.top;
			var obj = getTableFromPosition(midX,midY);
			
			var diffY = document.moveTable.elOffset.top - ui.offset.top;
			var diffX = Math.abs(document.moveTable.elOffset.left - ui.offset.left);
			var max = parseInt(document.moveTable.newEl.css("max-height").replace("px",""));
			var newHeight = document.moveTable.initHeight + diffY;
			if(diffY<-5 || diffX > 7 || obj==null || !obj.equals(document.moveTable.el.parents("[name=elmTable]:first"))) newHeight = max; 
			document.moveTable.newEl.css("height",newHeight);

			$("[name=layer]").remove();
			if(obj!=null){
				var tmpTable = obj.find(".Table_Pos");
				if($(tmpTable).offset()!=null){
					var newTmpTable = $("<div name='layer' tableid='"+tmpTable.attr("tableid")+"'></div>");
					newTmpTable.css("position", "absolute");
					newTmpTable.css({ opacity: 0.6 });
					newTmpTable.css("height",$(tmpTable).height());
					newTmpTable.css("width" , $(tmpTable).width());
					newTmpTable.css("top", $(tmpTable).offset().top);
					newTmpTable.css("left", $(tmpTable).offset().left);
					newTmpTable.css("background", "#f1eff2");
					newTmpTable.css("border","1px solid #6b88c5");
					$(event.target).before(newTmpTable);
				}
			}
		},
		stop: function( event, ui ) {
			if($("[name=layer]").size()==0){
				simpleDialog("Cannot move this reservation",null,cancelMoveTable);
			}
			else{
				var posresid = document.moveTable.el.attr("posresid");
				var tableid = $("[name=layer]").attr("tableid");
				//callservice
			}
		}});
}

function cancelMoveTable(){
	document.moveTable.newEl.remove();
	$("[name=layer]").remove();
	$("[name=tmpBlock]").remove();
	document.moveTable.el.css("display","");
	delete document.moveTable;
}

function getTableFromPosition(positionX,positionY)
{
	var all = $("[name=elmTable]");
	for(var i=0;i<all.length;i++)
	{
		var minX = $(all[i]).offset().left;
		var maxX = minX + $(all[i]).width();
		var minY = $(all[i]).offset().top;
		var maxY = minY + $(all[i]).height();
		if(minX <= positionX && positionX <= maxX &&
			minY <= positionY && positionY <= maxY)
			return $(all[i]);
	}
	return null;
}